class FriendshipsController < ApplicationController

  before_filter :load_member ,:only => [:show,:index]
  
  def initialize
    @styleName = "my_friend"
  end

  def index
  end

  def show
    @friendships = @member.friendships.paginate_by_member_id params[:id], :page => params[:page]
    @friends = @friendships.collect { |friend| Member.find friend[:friend_id]  }

    if request.xhr?
      render :update do |page|
        page.replace_html "friends_show", :partial => "/shared/profile_nav"
      end
    end
  end

  def create
    if request.xhr?
      friendship = current_accounts.member.friendships.build(:friend_id => params[:id])

      if friendship.save
        msg = 'Friendship was successfully created.'
      else
        msg = 'Friendship was failed created.'
      end
      
      render :update do |page|
        page.alert(msg)
      end
    end
  end

  # DELETE /friendships/1
  # DELETE /friendships/1.xml
  def destroy
    @friendship = Friendship.find_by_member_id_and_friend_id(current_acoounts.member,params[:id])
    @friendship.destroy

    respond_to do |format|
      format.html { redirect_to(friendships_url) }
      format.xml  { head :ok }
    end
  end

  protected

  def load_member
    @member = Member.find params[:id]
  end
  
end
